package com.xm.xmapp.customview;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.request.bean.Submit_order_commentRequest;
import com.xm.xmapp.response.bean.OrderBeanResponse;
import com.xm.xmapp.tools.StringTools;

public class PinJiaTipDialog extends Dialog implements View.OnClickListener {
	private Context context;
	private OrderBeanResponse t;
	private MyDialogButton in;
	private ImageView img[] = new ImageView[5];
	private TextView miaoshu,tijiao;
	private ImageView  hide;
	private int order_star;
	private EditText content;
	private LoadingDialog promptDialog;

	public PinJiaTipDialog(Context context, OrderBeanResponse t, MyDialogButton in) {
		super(context, R.style.Dialog);
		promptDialog = new LoadingDialog(context);
		this.context = context;
		this.t = t;
		this.in = in;
		order_star = 5;

	}

	@SuppressLint("InflateParams")
	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_pinjia_tip, null);
		img[0] = (ImageView) view.findViewById(R.id.img0);
		img[1] = (ImageView) view.findViewById(R.id.img1);
		img[2] = (ImageView) view.findViewById(R.id.img2);
		img[3] = (ImageView) view.findViewById(R.id.img3);
		img[4] = (ImageView) view.findViewById(R.id.img4);
		tijiao = (TextView) view.findViewById(R.id.tijiao);
		miaoshu = (TextView) view.findViewById(R.id.miaoshu);
		hide = (ImageView) view.findViewById(R.id.hide);
		content = (EditText) view.findViewById(R.id.content);
		TextView name = (TextView) view.findViewById(R.id.name);
		name.setText(t.car_number);

		img[0].setOnClickListener(this);
		img[1].setOnClickListener(this);
		img[2].setOnClickListener(this);
		img[3].setOnClickListener(this);
		img[4].setOnClickListener(this);
		tijiao.setOnClickListener(this);
		hide.setOnClickListener(this);

		setImg();
		setContentView(view);

	}

	private void setImg() {

		for (int i = 0; i < img.length; i++) {
			if (order_star >= (i + 1)) {
				img[i].setImageResource(R.mipmap.order_img_start_press);
			} else {
				img[i].setImageResource(R.mipmap.order_img_start_normal);
			}
		}
		miaoshu.setText(StringTools.getCommentText(order_star));

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		init();

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.hide:
			dismiss();
			break;
		case R.id.img0:
			order_star = 1;
			setImg();
			break;
		case R.id.img1:
			order_star = 2;
			setImg();
			break;
		case R.id.img2:
			order_star = 3;
			setImg();
			break;
		case R.id.img3:
			order_star = 4;
			setImg();
			break;
		case R.id.img4:
			order_star = 5;
			setImg();
			break;
		case R.id.tijiao:
			submit_order_commentRequest();
			break;
		default:
			break;
		}
	}

	private void submit_order_commentRequest() {

		Submit_order_commentRequest requestBean = new Submit_order_commentRequest();
		requestBean.order_id = t.order_id;
		requestBean.order_star = order_star;
		requestBean.content = content.getText().toString();

		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				dismiss();
				in.setTitle(1, null);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
			}
		});
	}
}
